<template>
  <div  class="app-container">
     <!-- 查询 -->
     <el-form  :inline="true" :model="query" size="mini"  class="demo-form-inline">
      <el-form-item label="标签名称：">
        <el-input v-model="query.name" ></el-input>
      </el-form-item>
      <el-form-item label="分类名称：">
        <el-select clearable filterable v-model="query.status" >
          <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-permission="'label:search'" icon='el-icon-search' type="primary" @click="queryData">查询</el-button>
        <el-button icon='el-icon-refresh' @click="reload">重置</el-button>
        <el-button v-permission="'label:add'" icon="el-icon-circle-plus-outline" type="primary" @click="openAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table
      :data="list"
      border
      stripe
      style="width: 100%">
      <el-table-column
        align="center"
        type=index
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column 
        align="center"
        prop="name"
        label="标签名称"
        width="300">
      </el-table-column>
      <el-table-column
        align="center"
        prop="categoryName"
        label="分类名称"
        width="300">
      </el-table-column>
      <el-table-column  
        align="center"   
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            v-permission="'label:edit'"
            @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            v-permission="'label:delete'"
            @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>   

    <!-- 分页功能 -->   
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total">
    </el-pagination>

    <!-- 弹出窗口 -->
    <edit :visible="edit.visible" :formData="edit.formData" :remoteClose="remoteClose" :title="edit.title" :categoryList="categoryList"></edit>
  </div>
</template>

<script>
import api from '@/api/label'
import Edit from './edit.vue'
export default {
  name:'Label',
  components: { Edit },
  data() {
    return {
      list:[],//数据
      page:{ // 分页相关
        total:0, // 总记录数
        size:20,// 每页显示20条数据
        current:1// 当前页码
      },
      query:{},// 查询条件
      categoryList: [], // 数组存储正常状态的分类
      edit:{
        visible:false,
        formData:{
          name:'',
          categoryId:null,
        },
        title:''
      }
    }
  },
  methods: {
    // 请求数据
    fetchData(){
      api.getList(this.query,this.current,this.size).then(
        responce=>{
          this.list=responce.data.records
          this.page.total=responce.data.total
          
        }
      )
    },
    // 编辑
    handleEdit(id){
      api.getById(id).then((response)=>{
          
          if (response.code === 20000){
            this.edit.formData=response.data
            this.edit.title = '编辑'
            this.edit.visible = true
            
          }
        })
    },
    // 删除
    handleDelete(id){
      this.$confirm('确认删除这条记录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          api.deleteById(id).then((response)=>{
              this.$message({
              type: response.code===20000?'success':'error',
              message: response.message
            });
          })
          this.fetchData()
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //修改个数
    handleSizeChange(val) {       
        this.page.size=val
        this.fetchData()
    },

    // 修改当前页
    handleCurrentChange(val) {      
      this.page.current=val
      this.fetchData()
    },
    //获取所有状态为正常的分类，用于条件查询中
    getCategoryList(){
      api.getNormalList().then(response=>{
        if(response.code===20000){
          this.categoryList=response.data
        }else{
          console.log('获取失败');
        }
      })
    },
    // 查询功能
    queryData() {
        this.page.current=1
        this.fetchData()
    },
    // 重载
    reload(){
      this.query={}
      this.fetchData()
    },

    // 触发关闭弹出的新增修改子组件窗口
    remoteClose(){
      this.edit.visible=false
      this.edit.formData={}
      this.fetchData()

    },
    // 弹出新增窗口
    openAdd(){
      this.edit.visible=true
      this.edit.title='新增'
      
    }
  },

  created() {
    this.fetchData()
    this.getCategoryList()
  },
}
</script>

<style>

</style>